import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/dao/course_dao.dart';
import 'package:hktk_app/dao/industry_dao.dart';

/// 听课记录列表页面，按时间排序，可删除记录

class VideoRecordList extends StatefulWidget {
  VideoRecordList({super.key});

  @override
  State<StatefulWidget> createState() => _VideoRecordListState();
}

class _VideoRecordListState extends State<VideoRecordList> {
  List<dynamic> _list = [];
  bool _isLoading = true;

  @override
  void initState() {
    //   initState
    super.initState();
    //   获取听课记录
    CourseDao.getVideoRecords(
            industryCode: IndustryDao.getExamKey().industryId,
            examId: IndustryDao.getExamKey().examId,
            current: 1,
            size: 100)
        .then((res) {
      if (mounted) {
        setState(() {
          _list = res["records"] as List<dynamic>;
          _isLoading = false;
        });
      }
    }).catchError((err) {
      if (mounted) {
        setState(() {
          _isLoading = false;
        });
      }
    });
  }

  @override
  void dispose() {
    //   dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('听课记录'),
      ),
      body: SafeArea(
          child: Container(
        margin: EdgeInsets.all(16),
        height: 1.sh -
            ScreenUtil().statusBarHeight -
            ScreenUtil().bottomBarHeight -
            kToolbarHeight -
            32,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10)),
        ),
        child: _isLoading
            ? Center(child: CircularProgressIndicator())
            : SingleChildScrollView(
                child: _list.isEmpty
                    ? Center(
                        child: SvgPicture.asset(
                          "images/home/empty.svg",
                          width: 1.sw - 36,
                        ),
                      )
                    : Column(children: [
                        for (var item in _list)
                          Container(
                            margin: EdgeInsets.all(12),
                            child: Row(
                              mainAxisSize: MainAxisSize.max,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  spacing: 16,
                                  children: [
                                    if (item["coverImg"] != null &&
                                        item["coverImg"].isNotEmpty)
                                      Image.network(
                                        item["coverImg"],
                                        width: 120,
                                        height: 90,
                                        fit: BoxFit.cover,
                                      ),
                                    SizedBox(
                                      width: 140,
                                      height: 40,
                                      child: Text(item["name"] ?? "",maxLines:2,style: TextStyle(overflow: TextOverflow.ellipsis,)),

                                    )
                                  ],
                                ),
                                TextButton(onPressed: () {

                                }, child: Text("前往",style: TextStyle(color: Colors.blue),))
                              ],
                            ),
                          )
                      ])),
      )),
    );
  }
}
